<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理</title>
   <script src="My.js"></script>
 <script type="text/javascript">
 
function searchStudent()
{
	if (window.XMLHttpRequest) 
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	     xmlhttp=new XMLHttpRequest(); 
	  } 
	else {// code for IE6, IE5 
	   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
	}
   name=document.getElementById("name").value;
   let majorID=document.getElementById("majorID").value;
   xmlhttp.open("get","/searchstudent?name="+name+"&majorID="+majorID,true);
   xmlhttp.onreadystatechange=function () 
   { if (xmlhttp.readyState === 4) {
      if (xmlhttp.status === 200) {
         try {
            const json = JSON.parse(xmlhttp.responseText);
            document.getElementById("infos").innerHTML = display(json);
         } catch (error) {
            console.error("解析 JSON 数据失败:", error);
            alert("解析数据失败，请检查服务器返回的数据格式");
         }
      } else {
         console.error("请求失败，状态码:", xmlhttp.status, "状态文本:", xmlhttp.statusText);
         alert("查询失败");
      }
   }
   }
  xmlhttp.send();
}




function display(jsons)
{
	content="<table border='1'>";
	content=content+"<tr><th>序号</th><th>学号</th><th>姓名</th><th>专业</th><th>身份证号</th><th>年龄</th><th>总学分</th><th>入学时间</th><th>性别</th><th>操作</th></tr>";
	for(i=0;i<jsons.length;i++)
	 {
		content+="<tr><td>"+(i+1)+"</td>";
		content+="<td>"+jsons[i].stuID+"</td>";
		content+="<td>"+jsons[i].name+"</td>";
		content+="<td>"+jsons[i].majorID+"</td>";
		content+="<td>"+jsons[i].sfzh+"</td>";
		age=getAge(jsons[i].sfzh);
		content+="<td>"+age+"</td>";

		content+="<td>"+jsons[i].total+"</td>";
		content+="<td>"+jsons[i].enrollTime+"</td>";
		content+="<td>"+jsons[i].sex+"</td>";
		content+="<td><button onclick='deleteStudent(\""+jsons[i].stuID+"\")'>删除</button>"
		content+="<button onclick='updateStudent(\""+jsons[i].stuID+"\",\""+jsons[i].majorID+"\",\""+jsons[i].enrollTime+"\",\""+jsons[i].sfzh+"\")'>修改</button></td></tr>"
	 }
	content+="</table>"
	return content;
}


function deleteStudent(stuID) {
   if (!confirm("确定要删除账号：" + stuID + " 用户信息吗?")) return;
   if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
   } else {// code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.open("get", "/deletestudent?stuID=" + stuID, true);
   xmlhttp.onreadystatechange=function ()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
         if(xmlhttp.responseText=="1")
         {
            alert("删除成功！");
            searchStudent();
         }
         else{
            alert("删除失败！")
         }

      }
   }
   xmlhttp.send();

}



function updateStudent(stuID,majorID,enrollTime,sfzh) {
   if (!confirm("确定要修改账号：" + stuID + " 用户信息吗?")) return;
   location.href = "updateStudent.html?stuID=" + stuID + "&majorID=" + majorID+"&enrollTime="+enrollTime+"&sfzh="+sfzh;
}

function newStudent(){
   location.href = "newStudent.html";
}



</script>
</head>
<body onload="searchStudent()">
   <div align="center">
      <h1>所有学生信息</h1>

<!--      新增学生按钮  提交表单跳转-->
         <button id="newStudent" onclick="newStudent()">新增</button>


<!--         姓名输入框-->
         <label for="name">姓名：</label>
         <input type="text" id="name" name="name"  placeholder="请输入姓名">
<!--         专业下拉框-->
         <label for="majorID">专业号：</label>
         <select id="majorID" name="majorID" required>
            <option value="81">计算机科学与技术</option>
            <option value="82">电子信息工程</option>
            <option value="83">英语</option>
            <option value="" selected> </option>
         </select>
         <button id="searchButton" onclick="searchStudent()">查询</button>

<!--      查询结果-->
      <div id="infos"></div>
   </div>
</body>
</html>